package com.newland.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newland.rbac.dao.UserMapper;
import com.newland.rbac.model.User;
import com.newland.rbac.service.UserService;
import com.newland.rbac.utils.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yanan
 * @since 2021-07-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean save(User entity) {
        String salt = SaltUtil.getSalt(8);
        entity.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(entity.getPwd(), salt, 1024);
        entity.setPwd(md5Hash.toHex());
        userMapper.insert(entity);
        return false;
    }

    @Override
    public List<String> findRolesByPrincipal(String principal) {
        return userMapper.findRolesByPrincipal(principal);
    }

    @Override
    public List<String> findApisByPrincipal(String principal) {
        return userMapper.findApisByPrincipal(principal);
    }
}
